package br.com.gbvbahia.mybooks;

import android.os.AsyncTask;
import android.support.v4.app.FragmentActivity;
import br.com.gbvbahia.commons.Transacao.Transacao;
import br.com.gbvbahia.commons.Transacao.TransacaoTask;
import br.com.gbvbahia.commons.utils.AndroidUtils;

public abstract class ActivityMae extends FragmentActivity{
	public static final String LOG_TAG = "mybook";
	private TransacaoTask task;
	protected void alert(int mensagem){
		AndroidUtils.alertDialog(this, mensagem, R.string.app_name);
	}
	
	// Inicia a thread
	public void startTransacaoComRede(Transacao transacao) {
		boolean redeOk = AndroidUtils.isNetworkAvailable(this);
        if(redeOk) {
        	// Inicia a transacao
    		task = new TransacaoTask(this, transacao , R.string.aguarde, R.string.app_name);
    		task.execute();        	
        } else {
        	// Nao existe conexao
        	AndroidUtils.alertDialog(this, R.string.aviso_13, R.string.app_name);
        }
	}
	
	// Inicia a thread
	public void startTransacao(Transacao transacao) {
    		TransacaoTask task = new TransacaoTask(this, transacao , R.string.aguarde, R.string.app_name);
    		task.execute();        	
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(task != null) {
			boolean executando = task.getStatus().equals(AsyncTask.Status.RUNNING);
			if(executando) {
				task.cancel(true);
				task.fecharProgress();
			}
		}
	}
}
